<%-- 
/*
Assignment 1
Class ID        : SE0691
Group           : 1
Due Date        : 20 November 2012
I declare that this assignment is my own work
in accordance with FPT Policy.
*/
--%>

<%@page import="edu.fpt.hib.Services"%>
<%@page import="edu.fpt.dal.ServicesDAL"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>
<%
    HttpSession newSession = request.getSession();
    if (newSession.getAttribute("currentAdmin") == null) {
        response.sendRedirect("../index.jsp");
    }
%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Create news services</title>
        <style type="text/css"> <!--@import url("../style.css");--></style>
        <script>function goBack()  {  window.history.back()  }</script>
    </head>
    <body>
        <jsp:include page="template/header.jsp"/>
        <div id="page">

            <% try {
                    if (request.getParameter("id") == null) {%>
            <h2 class="title">Create New Service</h2>
            <form name="create" method="POST" action="createservice.jsp">
                <table border="0">
                    <tr>
                        <td align="right">Service ID</td>
                        <td><input class="searchinput" type="text" name="id" 
                                   size="20" required="required" maxlength="10"></td>
                    </tr>
                    <tr>
                        <td align="right">Service Name</td>
                        <td><input class="searchinput" type="text" name="name" 
                                   size="20" required="required" maxlength="20"></td>
                    </tr>
                    <tr>
                        <td align="right">Service Price</td>
                        <td><input class="searchinput" type="text" name="price" 
                                   size="20" required="required"></td>
                    </tr>
                    <tr>
                        <td align="right">&nbsp;</td>
                        <td>
                            <p align="center"><input class="searchsubmit" 
                                                     type="submit" value="Create" 
                                                     name="B1">
                                <input class="searchsubmit" type="reset" value="Reset" 
                                       name="B2"></td>
                    </tr>
                </table>
            </form>
            <% } else {
                if (ServicesDAL.exist(request.getParameter("id"))) {%>
            <h4>Duplicated username! Please choose another</h4>
            <input type='button' value='Back' onclick='goBack()'>
            <%} else {
                String id = request.getParameter("id");
                String name = request.getParameter("name");
                double price = Double.parseDouble(request.getParameter("price"));
                Services ser = new Services(id, name, price);
                ServicesDAL.insert(ser);
            %>
            <h2 class="title">Successful</h2><br>

            <%}
                    }
                } catch (Exception e) {
                    out.println(e.getMessage());
                }
            %>
        </div>
        <jsp:include page="template/footer.jsp"/>
</html>
